package com.itzhigui.service.impl;

import com.itzhigui.entity.Cart;
import com.itzhigui.entity.Product;
import com.itzhigui.mapper.CartMapper;
import com.itzhigui.mapper.ProductMapper;
import com.itzhigui.service.CartService;
import com.itzhigui.utils.ResponseEnum;
import com.itzhigui.utils.ResponseUtil;
import com.itzhigui.vo.CartVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author java1412
 * @since 2023-11-14 17:22:21
 */
@Service
@Transactional
public class CartServiceImpl implements CartService {

    @Resource
    private CartMapper cartMapper;

    @Resource
    private ProductMapper productMapper;

    @Override
    public ResponseUtil findCountByUserId(Integer userId) {
        // 查询数量
        Integer count = cartMapper.findCountByUserId(userId);
        return ResponseUtil.get(ResponseEnum.CART_FIND_COUNT, count);
    }

    @Override
    public ResponseUtil findCartByUser(Integer userId) {
        // 获取购物车的数据,但是cart中没有商品信息
        List<Cart> cartList = cartMapper.findCartByUserId(userId);
        // 将cartList数据转换成CartVoList的数据
        List<CartVo> cartVoList = new ArrayList();
        cartList.forEach(cart -> {
            // 将cart对象的数据复制cartVo
            CartVo cartVo = new CartVo();
            // BeanUtils.copyProperties()——将第一个参数对象中属性值复制第二个参数对象中的同名
            BeanUtils.copyProperties(cart, cartVo);
            // 根据cart中商品的编号查询商品信息
            Product product = productMapper.findById(cart.getProductId());
            // 将product对象中的name和price复制一份到cartVo中
            cartVo.setName(product.getName());
            cartVo.setPrice(product.getPrice());
            cartVoList.add(cartVo);
        });
        return ResponseUtil.get(ResponseEnum.CART_FIND_CART_INFO, cartVoList);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ResponseUtil updateCart(Integer id, Integer quantity, Integer checked) {
        Integer result = cartMapper.updateCart(id, quantity, checked);
        return result.intValue() > 0
                ? ResponseUtil.get(ResponseEnum.CART_UPDATE_CART_SUCCESS)
                : ResponseUtil.get(ResponseEnum.CART_UPDATE_CART_FAILURE);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ResponseUtil deleteCartById(Integer id) {
        Integer result = cartMapper.deleteById(id);
        return result.intValue() > 0
                ? ResponseUtil.get(ResponseEnum.CART_DELETE_CART_SUCCESS)
                : ResponseUtil.get(ResponseEnum.CART_DELETE_CART_FAILURE);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ResponseUtil deleteCartByIds(Integer[] ids) {
        Integer result = cartMapper.deleteByIds(ids);
        return result.intValue() > 0
                ? ResponseUtil.get(ResponseEnum.CART_DELETE_CART_SUCCESS)
                : ResponseUtil.get(ResponseEnum.CART_DELETE_CART_FAILURE);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ResponseUtil truncateCartByUserId(Integer userId) {
        Integer result = cartMapper.deleteByUserId(userId);
        return result.intValue() > 0
                ? ResponseUtil.get(ResponseEnum.CART_DELETE_CART_SUCCESS)
                : ResponseUtil.get(ResponseEnum.CART_DELETE_CART_FAILURE);
    }
}
